/**
 * ===========================================================================
 * 版权所有 湖南免了么网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.mianle.me
 * ===========================================================================
 * 11:17 2018/7/9
 */
package com.mianle.userapi.service.delayed.demo1;

import java.util.Date;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Author: yiyuan
 * @Description:
 * @Date: Created in 11:17 2018/7/9
 *
 */
public class Main3 {


        public static void main(String[] args) {
            DelayQueue<Event2> delayQueue = new DelayQueue<Event2>();
            Thread[] threads = new Thread[5];

            for (int i = 0; i < threads.length; i++) {
                Task task2 = new Task(i + 1, delayQueue);
                threads[i] = new Thread(task2);
            }

            for (int i = 0; i < threads.length; i++) {
                threads[i].start();
            }

            for (int i = 0; i < threads.length; i++) {
                try {
                    threads[i].join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            do {
                int counter = 0;
                Event2 event2;
                do {
                    event2 = delayQueue.poll();
                    if (event2 != null)
                        counter++;
                } while (event2 != null);
                System.out.printf("At %s you have read %d events\n", new Date(),
                        counter);
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (delayQueue.size() > 0);
        }
    }


